package com.yuntsg.ruiijn.paperana.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuntsg.nnsfcp.common.utils.UserUtil;
import com.yuntsg.nnsfcp.front.entity.Employee;
import com.yuntsg.nnsfcp.front.mapper.EmployeeMapper;
import com.yuntsg.ruiijn.database.entity.PaperAna;
import com.yuntsg.ruiijn.database.entity.PaperAnaTodo;
import com.yuntsg.ruiijn.database.mapper.PaperAnaMapper;
import com.yuntsg.ruiijn.database.mapper.PaperAnaTodoMapper;
import com.yuntsg.ruiijn.paperana.service.MatchService;
import com.yuntsg.nnsfcp.common.beans.ResultBean;
import common.util.CheckUtil;
import common.util.Code;
import com.yuntsg.nnsfcp.common.utils.RedisUtil;
import common.util.StringUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: gcr
 * @Date: 2023/9/6 9:31
 */
@Slf4j
@RestController

@RequestMapping("/papersearch")
@SuppressWarnings("ALL")
public class SearchController {


    @Resource
    UserUtil userUtil;
    @Resource
    RedisUtil redisUtil;

    @Resource
    EmployeeMapper employeeMapper;

    @Resource
    MatchService matchService;

    @Resource
    PaperAnaMapper paperAnaMapper;

    @Resource
    PaperAnaTodoMapper paperAnaTodoMapper;
    @Resource
    HttpServletRequest request;

    @GetMapping("/gettestout")
    public ResultBean<Object> outputinfo() {
        String realPath = request.getServletContext().getRealPath("");
        log.info("realPath++" + realPath);
        return new ResultBean<>("");
    }

    //  2023年9月6日09:32:03 进行检索查询上传等操作

    //@ApiOperation(value = "纯文字进行查重")
    @PostMapping("/add-article")
//    @ApiImplicitParams(
//            {
//                    @ApiImplicitParam(name = "pushFlag", value = "是否已发表"),
//                    @ApiImplicitParam(name = "str", value = "上传的文本内容"),
//                    @ApiImplicitParam(name = "deptId", value = "科室ID,当前文章在哪个科室下"),
//                    @ApiImplicitParam(name = "annex", value = "附件"),
//                    @ApiImplicitParam(name = "master", value = "导师"),
//                    @ApiImplicitParam(name = "author", value = "归属作者"),
//                    @ApiImplicitParam(name = "pmorut", value = "要排除的pm或者ut号"),
//                    @ApiImplicitParam(name = "belongDate", value = "归属日期"),
//                    @ApiImplicitParam(name = "title", value = "文章标题"),
//
//            })
    public ResultBean<Object> addArticle(String str, Integer masterId,
                                         Integer pushFlag,
                                         String annex,
                                         String master, String author, String belongDate,
                                         String title, Integer wosId, Integer uid, String key, Integer type, String pmorut) {

        //采用循环查询方式进行 一瞬间不会得到结果
        //type =1 是指的来自张福垒的数据过来的
        System.out.println("123");
        if (type != null && type == 1) {
            System.out.println("参数文本长度二___" + str.length());
            log.info("福垒一键分析过来的");
            if (uid == null) {
                uid = userUtil.getUid();
            }
            Employee sysUser = employeeMapper.selectById(uid);
            CheckUtil.check(StringUtil.isNotNull(str), "上传文件内容为空");
            long date = System.currentTimeMillis();
            PaperAna paperAna = paperAnaMapper.selectOne(new QueryWrapper<PaperAna>().eq("rekey", key));
            if (paperAna == null) {
                PaperAna paperAnaNew = new PaperAna();
                paperAnaNew.setUploadName(sysUser.getUserName());
                paperAnaNew.setWosId(wosId);
                paperAnaNew.setPushFlag(pushFlag);
                paperAnaNew.setAnnex(annex);
                paperAnaNew.setMaster(master);
                paperAnaNew.setMasterId(masterId);
                paperAnaNew.setAuthor(author);
                paperAnaNew.setBelongDate(belongDate);
                paperAnaNew.setUid(uid);
                paperAnaNew.setTitle(title);
                paperAnaNew.setRekey(key);
                paperAnaNew.setContent(str);
                paperAnaNew.setStatus(0);
                paperAnaNew.setAdmCheck(0);
                paperAnaNew.setUptime(System.currentTimeMillis());
                paperAnaNew.setIsDel(0);
                paperAnaNew.setSysCreateTime(LocalDateTime.now());
                paperAnaNew.setSysUpdateTime(LocalDateTime.now());
                paperAnaMapper.insert(paperAnaNew);
            }
            matchService.caculateStr("", "", "", str, key, uid, pmorut, null);

        } else {
            log.info("gcr 文章分析过来的");
            if (uid == null) {
                uid = userUtil.getUid();
            }
            Employee sysUser = employeeMapper.selectById(uid);
            CheckUtil.check(StringUtil.isNotNull(str), "上传文件内容为空");
            long date = System.currentTimeMillis();
            key = Code.digestData(date + uid + "paperana");
            PaperAna paperAna = paperAnaMapper.selectOne(new QueryWrapper<PaperAna>().eq("uid", uid).eq("rekey", key));
            if (paperAna == null) {
                PaperAna paperAnaNew = new PaperAna();
                paperAnaNew.setUploadName(sysUser.getUserName());
                paperAnaNew.setWosId(wosId);
                paperAnaNew.setPushFlag(pushFlag);
                paperAnaNew.setAnnex(annex);
                paperAnaNew.setMaster(master);
                paperAnaNew.setMasterId(masterId);
                paperAnaNew.setAuthor(author);
                paperAnaNew.setBelongDate(belongDate);
                paperAnaNew.setUid(uid);
                paperAnaNew.setTitle(title);
                paperAnaNew.setRekey(key);
                paperAnaNew.setContent(str);
                paperAnaNew.setStatus(0);
                paperAnaNew.setAdmCheck(0);
                paperAnaNew.setUptime(System.currentTimeMillis());
                paperAnaNew.setIsDel(0);
                paperAnaNew.setSysCreateTime(LocalDateTime.now());
                paperAnaNew.setSysUpdateTime(LocalDateTime.now());
                paperAnaMapper.insert(paperAnaNew);
            }
            matchService.caculateStr("", "", "", str, key, uid, pmorut, null);

        }

        return new ResultBean<>(key);
    }

//    //获取结果
//    @GetMapping("/get-res")
//    public ResultBean<Object> getRes(String key) {
//        Object object = redisUtil.getObject(key);
//        CheckUtil.check(StringUtil.isNotNull(object), "还未生成,请等待或者key错误");
//        if (object != null) {
//            OutEntity res = (OutEntity) object;
//            return new ResultBean<>(res);
//        }
//        return new ResultBean<>("");
//    }


//    @GetMapping("/do-generateword")
//    public ResultBean<Object> dogenerateWord(Integer id) {
//        CheckUtil.check(StringUtil.isNotNull(id), "id不能是空");
//        matchService.geneWord(id);
//        return new ResultBean<>("");
//    }

    //自定义上传一对多对比
    @PostMapping("manual-check")
//    @ApiImplicitParams(
//            {
//                    @ApiImplicitParam(name = "org", value = "原始数据"),
//                    @ApiImplicitParam(name = "compare", value = "待比对的数据集"),
//            })
    public ResultBean<Object> manualCheckStr(String org, @RequestParam(value = "compare[]") String[] compare) {
        Employee sysUser = employeeMapper.selectById(userUtil.getUid());
        matchService.InsTempEs(compare, org, sysUser);
        return new ResultBean<>("已上传 请稍等计算");
    }

    //   journalctl -f -u searchhub_ruijin
    //  service searchhub_ruijin restart


    //获取列表页
    @GetMapping("/list")
    public ResultBean<Object> list(Integer page, Integer limit) {
        if (page == null || page == 0) {
            page = 1;
            limit = 10;
        }
        LambdaQueryChainWrapper<PaperAna> queryChainWrapper = new LambdaQueryChainWrapper<>(paperAnaMapper);
        queryChainWrapper.isNull(PaperAna::getWosId);
        Page<PaperAna> anaPage = queryChainWrapper.page(new Page<>(page, limit));
        for (PaperAna record : anaPage.getRecords()) {
            try {
                String resultWord = record.getResultWord();
                resultWord = resultWord.replace("/home/analysis/uploadFile/template/", "");
                record.setResultWord(resultWord);
                String resultPdf = record.getResultPdf();
                if (StringUtil.isNotNull(resultPdf)) {
                    resultPdf = resultPdf.replace("/home/analysis/uploadFile/template/", "");
                    record.setResultPdf(resultPdf);
                }
            } catch (Exception e) {
                log.error("赋值文字版报告出错");
            }
        }
        return new ResultBean<>(anaPage);
    }

    //获取列表页
    @GetMapping("/list-manual")
    public ResultBean<Object> listManual(Integer page, Integer limit) {
        //自定义分析查看
        if (page == null || page == 0) {
            page = 1;
            limit = 10;
        }
        LambdaQueryChainWrapper<PaperAnaTodo> queryChainWrapper = new LambdaQueryChainWrapper<>(paperAnaTodoMapper);
        //如果是委托 则只能看到自己名下的
        Page<PaperAnaTodo> anaPage = queryChainWrapper.page(new Page<>(page, limit));
        for (PaperAnaTodo record : anaPage.getRecords()) {
            try {
                String resultWord = record.getResultWord();
                resultWord = resultWord.replace("/home/analysis/uploadFile/template/", "");
                record.setResultWord(resultWord);
                String resultPdf = record.getResultPdf();
                resultPdf = resultPdf.replace("/home/analysis/uploadFile/template/", "");
                record.setResultPdf(resultPdf);
            } catch (Exception e) {
            }
        }
        return new ResultBean<>(anaPage);
    }

//    //@ApiOperation(value = "02-将文章指派给委托人")
//    @GetMapping("assign")
//    public ResultBean<Object> assign(Integer wosId, Integer uid) {
//        SysUser sysUser = userMapper.selectById(uid);
//        String realName = sysUser.getRealName();
//
//        PaperAna paperAna = paperAnaMapper.selectById(wosId);
//        paperAna.setDeptEntrust(realName);
//        paperAna.setDeptId(sysUser.getDeptId());
//        paperAna.setDeptEntrustFinishFlag(0);
//        paperAnaMapper.updateById(paperAna);
//        return new ResultBean<>("操作完成");
//    }


//    //@ApiOperation(value = "03-委托人完成工作,结束流程")
//    @GetMapping("finish")
//    public ResultBean<Object> finish(Integer wosId) {
//        PaperAna paperAna = paperAnaMapper.selectById(wosId);
//        paperAna.setDeptEntrustFinishFlag(1);
//        paperAnaMapper.updateById(paperAna);
//        return new ResultBean<>("操作完成");
//    }


}
